package com.scitc.pleasurevideo;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;

import com.scitc.pleasurevideo.adapter.CachingGridviewAdapter;
import com.scitc.pleasurevideo.data.HistoryTvDataMan;
import com.scitc.pleasurevideo.helper.ForwardHelper;
import com.scitc.pleasurevideo.pojo.CachingTvVo;
import com.scitc.pleasurevideo.pojo.DoubanTvVo;
import com.scitc.pleasurevideo.pojo.MenuTvVo;
import com.scitc.pleasurevideo.util.OnClickUtil;
import com.scitc.pleasurevideo.view.CoordGridView;
import com.scitc.pleasurevideo.view.SwDialog;

import java.util.ArrayList;
import java.util.List;

import static com.scitc.pleasurevideo.Tabhome.chingeAppbarLayout;


/**
 * @author 853
 */
public class HistoryFragment extends Fragment {

    private RelativeLayout searchBar;
    private Context context;
    private ConstraintLayout nullHistoryItem;
    private CoordGridView mGridView;
    private CachingGridviewAdapter madapter;
    private List<CachingTvVo> items = new ArrayList<>();

    private RelativeLayout rlHistoryGridview;
    private TextView numHistory;
    private ImageView igClearHistory;
    private TextView tvMakePlan;


    public static HistoryFragment newInstance() {
        Bundle args = new Bundle();
        HistoryFragment fragment = new HistoryFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private static JumpCategoryFragment jumpCategoryFragment;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_history, container, false);
        context = getContext();
        initView(view);
        onClick();
        loadHistoryTv();
        return view;
    }

    /**
     * 进入这个界面时
     * 加载历史数据以及初始化状态栏是白底黑字
     */
    @Override
    public void onResume() {
        super.onResume();
        loadHistoryTv();
        chingeAppbarLayout.OnChingeAppbarLayout(1);
    }

    /**
     * 加载历史数据
     */
    private void loadHistoryTv() {

        List<CachingTvVo> cachingTvVos = HistoryTvDataMan.getHistoryTvDataMan().seleteHistoryTvDataMan();

        if (cachingTvVos == null || cachingTvVos.size() < 1) {
            setNullHistoryItemVisity();
            return;
        } else {
            setNullHistoryItemGone();
        }

        madapter.addAll(cachingTvVos);
        numHistory.setText("共" + cachingTvVos.size() + "个记录");
    }

    /**
     * 列表的点击事件
     * @param cachingTvVo
     */
    private void mgridViewClick(CachingTvVo cachingTvVo) {
        OnClickUtil.setViewClickTimes(mGridView);
        DoubanTvVo doubanTvPojo = new DoubanTvVo();
        doubanTvPojo.setTitle(cachingTvVo.getName());
        doubanTvPojo.setCover(cachingTvVo.getImgUrl());

        MenuTvVo menuTvPojo = new MenuTvVo();
        menuTvPojo.setUrl(cachingTvVo.getUrl());
        menuTvPojo.setPlayerString(cachingTvVo.getPalyListUrl());

        ForwardHelper.toTvPlayMenuActivity((Activity) context, doubanTvPojo, menuTvPojo, cachingTvVo.getPlayMenuTvPojos());
    }

    /**
     * 点击事件
     */
    private void onClick() {
        //搜索bar的点击事件
        searchBar.setOnClickListener(view -> {
            OnClickUtil.setViewClickTimes(searchBar);
            ForwardHelper.toSearchActivity((Activity) context);
        });
        //列表的点击事件
        mGridView.setOnItemClickListener((adapterView, view, i, l) -> {
            CachingTvVo cachingTvVo = madapter.getItem(i);
            mgridViewClick(cachingTvVo);
        });
        //列表的长按事件
        mGridView.setOnItemLongClickListener((adapterView, view, i, l) -> {
            CachingTvVo news = madapter.getItem(i);
            showSwDialog(news);
            return true;
        });
        //清楚历史记录
        igClearHistory.setOnClickListener(view -> showItemLongClick());
        //当没有历史记录时，点击跳转到首页
        tvMakePlan.setOnClickListener(view -> jumpCategoryFragment.onJumpCategoryFragment());
    }

    /**
     * 显示对话框，是否全部删除历史记录
     */
    private void showItemLongClick(){
        SwDialog exitDialog = new SwDialog(context, "全部删除", "你确定要删除全部历史记录吗？");
        exitDialog.setOkListener(() -> {
            HistoryTvDataMan.getHistoryTvDataMan().clearHistoryTvDataMen();
            madapter.clearItem();
            setNullHistoryItemVisity();
        });
        exitDialog.show();
    }

    /**
     * 显示是否删除某一个历史记录
     * @param news
     */
    private void showSwDialog(final CachingTvVo news) {
        SwDialog exitDialog = new SwDialog(context, "删除操作", "你确定要删除 《" + news.getName() + "》 吗？");
        exitDialog.setOkListener(() -> {
            madapter.deleteItem(news.getId());
            HistoryTvDataMan.getHistoryTvDataMan().deleteHistoryTvDataMan(news.getId());
            checkItemNum();
        });
        exitDialog.show();
    }

    /**
     * 初始化控件
     * @param view
     */
    private void initView(View view) {
        searchBar = (RelativeLayout) view.findViewById(R.id.rl_query);
        nullHistoryItem = (ConstraintLayout) view.findViewById(R.id.nullHistoryItem);
        mGridView = (CoordGridView) view.findViewById(R.id.xGridView);
        rlHistoryGridview = (RelativeLayout) view.findViewById(R.id.rlHistoryGridview);
        numHistory = (TextView) view.findViewById(R.id.numHistory);
        igClearHistory = (ImageView) view.findViewById(R.id.igClearHistory);
        tvMakePlan = (TextView) view.findViewById(R.id.tvMakePlan);

        madapter = new CachingGridviewAdapter(context, items);
        mGridView.setAdapter(madapter);
    }

    /**
     * 检查列表还没有数据
     */
    private void checkItemNum() {
        if (madapter.getCount() == 0) {
            setNullHistoryItemVisity();
        }
    }

    /**
     * 设置列表没有数据时，显示提醒用户没有历史记录
     */
    private void setNullHistoryItemVisity() {
        nullHistoryItem.setVisibility(View.VISIBLE);
        rlHistoryGridview.setVisibility(View.GONE);
    }

    /**
     * 设置列表有数据时，显示列表
     */
    private void setNullHistoryItemGone() {
        nullHistoryItem.setVisibility(View.GONE);
        rlHistoryGridview.setVisibility(View.VISIBLE);
    }

    /**
     * fragment 显示界面的是回调
     * @param menuVisible
     */
    @Override
    public void setMenuVisibility(boolean menuVisible) {
        if(menuVisible){
            Tabhome.chingeAppbarLayout.OnChingeAppbarLayout(1);
        }
    }

    /**
     *回调跳转界面
     */
    public interface JumpCategoryFragment {
        void onJumpCategoryFragment();
    }

    public static void setOnJumpCategoryFragment(JumpCategoryFragment jumpCategory) {
        jumpCategoryFragment = jumpCategory;
    }
}
